﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Zyg.SSO.Core.Business
{
    /// <summary>
    /// The relationship between Account and App.
    /// </summary>
    public class AccountAppCollection : BusinessCollectionBase<AccountApp, Guid>
    {
        #region Fields

        private Account account = null;
        private App app = null;

        #endregion

        #region Constructors

        public AccountAppCollection()
        {
        }

        public AccountAppCollection(Account account, App app, RelationType relationType)
        {
            this.account = account;
            this.app = app;
            this.RelationType = relationType;
        }

        #endregion

        #region Override Properties

        private bool isChanged = false;
        /// <summary>
        /// Gets if this collection's data has been changed.
        /// </summary>
        public override bool IsChanged
        {
            get
            {
                if (isChanged == false)
                {
                    bool changed = false;

                    foreach (AccountApp accountApp in Elements)
                    {
                        if (accountApp.Users.IsChanged)
                        {
                            changed = true;
                            break;
                        }
                    }

                    return changed;
                }

                return isChanged;
            }
            protected set
            {
                isChanged = value;
            }
        }

        #endregion

        #region Override Methods

        protected override void Load()
        {
            Elements = Services.DALService.FillAccountApps(account, app);
            MarkOld();

            foreach (AccountApp aa in Elements)
            {
                aa.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(delegate(object sender, PropertyChangedEventArgs e)
                {
                    isChanged = true;
                });
            }
        }

        protected override void Update()
        {
            #region Method 1
            /*
            Services.DALService.DeleteUpdateAccountApps(account, app, Elements);

            for (int i = Elements.Count - 1; i > -1; i--)
            {
                Elements[i].MarkOld();
                Elements[i].Save();
            }
            */
            #endregion

            #region Method 2

            foreach (AccountApp account in Elements)
            {
                account.Save();
            }

            #endregion
        }

        #endregion
    }

    /// <summary>
    /// The user mapping between app and current system.
    /// </summary>
    public class AccoutAppUserCollection : BusinessCollectionBase<AccountAppUser, Guid>
    {
        #region Constructors

        public AccoutAppUserCollection()
        {
        }

        public AccoutAppUserCollection(AccountApp accountApp, RelationType relationType)
        {
            this.accountApp = accountApp;
            this.RelationType = relationType;
        }

        #endregion

        #region Fields and Properties

        private AccountApp accountApp;

        #endregion

        #region Override Methods

        protected override void Load()
        {
            Elements = Services.DALService.FillAccountAppUsers(accountApp);
            MarkOld();

            foreach (AccountAppUser aau in Elements)
            {
                aau.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(delegate(object sender, PropertyChangedEventArgs e)
                {
                    base.IsChanged = true;
                });
            }
        }

        protected override void Update()
        {
            foreach (Business.AccountAppUser aau in Elements)
            {
                aau.Save();
            }
        }

        public override void AddOnSave(AccountAppUser element)
        {
            element.AccountAppID = accountApp.Id;

            Elements.Add(element);

            IsChanged = true;

            OnCollectionChanged();
        }

        //public override void DeleteOnSave(AccoutAppUser element)
        //{
        //    for (int i = Elements.Count - 1; i > -1; i--)
        //    {
        //        if (Elements[i].AccountAppID == element.AccountAppID)
        //        {
        //            Elements[i].DeleteOnSave();

        //            base.IsChanged = true;

        //            OnCollectionChanged();
        //        }
        //    }
        //}

        public void DeleteOnSave(Guid id)
        {
            for (int i = Elements.Count - 1; i > -1; i--)
            {
                if (Elements[i].Id == id)
                {
                    Elements[i].DeleteOnSave();

                    base.IsChanged = true;

                    OnCollectionChanged();
                }
            }
        }

        #endregion
    }

    /// <summary>
    /// Accounts that have the same role.
    /// </summary>
    public class RoleAccountCollection : BusinessCollectionBase<Business.Account, System.Guid>
    {
        #region Fields and Properties

        private Role role;

        #endregion

        #region Constructors

        public RoleAccountCollection()
        {
        }

        public RoleAccountCollection(Role role, RelationType relationType)
        {
            this.role = role;
            this.RelationType = relationType;
        }

        #endregion

        #region Override Methods

        protected override void Load()
        {
            Elements = Services.DALService.FillAccounts(role);
            MarkOld();
        }

        protected override void Update()
        {
            foreach (Account account in base.Elements)
            {
                account.Save();
            }
        }

        public override void AddOnSave(Account element)
        {
            element.RoleID = role.Id;

            base.Elements.Add(element);

            base.IsChanged = true;

            OnCollectionChanged();
        }

        public override void DeleteOnSave(Account element)
        {
            for (int i = Elements.Count - 1; i > -1; i--)
            {
                if (Elements[i].Id == element.Id)
                {
                    Elements[i].RoleID = Business.Role.User.Id;

                    base.IsChanged = true;

                    OnCollectionChanged();
                }
            }
        }

        public void DeleteOnSave(Guid id)
        {
            for (int i = Elements.Count - 1; i > -1; i--)
            {
                if (Elements[i].Id == id)
                {
                    Elements[i].RoleID = Business.Role.User.Id;

                    base.IsChanged = true;

                    OnCollectionChanged();
                }
            }
        }

        public override void DeleteAtOnSave(int index)
        {
            if (index > -1 && index < Elements.Count)
            {
                Elements[index].RoleID = Business.Role.User.Id;

                base.IsChanged = true;

                OnCollectionChanged();
            }
        }

        #endregion
    }
}
